<template>
	<view class="index-content">
		<!--
		<web-view src="https://skrbtna.xyz/" :webview-styles="webviewStyles" ref="webviews"></web-view>
		-->
		<view class="top-input" v-show="shows==1" catchtouchmove>
			<view class="top-title">磁力网站保存</view>
			<view class="top-item">
				<input placeholder="请输入网站名字" v-model="name" class="input" />
			</view>
			<view class="top-item">
				<input placeholder="请输入网站网址" v-model="url" class="input" />
			</view>
		    <view class="submits" @click="saves()">确认添加</view>
		</view>
		<view class="moban" v-show="shows==1" :style="{height:height+'px'}" @click="closeadd()"></view>
		
		<view class="add-bts" @click="adds()"></view>
		<view class="centers">
		  <view class="items" @click="navToBts()">
			本地种子库
		 </view>	
		</view>
		<view class="centers" v-for="(val,index) in btlist">
			<view class="url-items" @click="navTos(val)">
				<view class="left" :style="{width:width+'px'}">{{val.name}}</view>
				<view class="dels" @click="dels(val,index)"></view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				name:'',
				url:'',
				width:'',
				text:'',
				texts:'',
				timer:'',
				shows:2,
				height:'',
				btlist:[
					{
					 url:'https://btsow.rest/search/',
					 name:'Btsow',
					 status:'1'
				    },
					/**
					{
					 url:'https://pornbt.net/',
					 name:'Pornbt',
					 status:'1'
					}**/
				],
				webviewStyles: { 
				   color: '#333', 
				}
			}
		},
		onLoad() {
			let self=this
			if(uni.getStorageSync('btlist')){
				self.btlist=uni.getStorageSync('btlist')
			}
			uni.getSystemInfo({
				success: function(res) {
					self.height = res.windowHeight
					self.width=res.windowWidth-88
				  }
			});
			plus.sqlite.openDatabase({
					name: 'first',
					path: '_doc/test.db',
					success: function(e){
						console.log('openDatabase success!');
					},
					fail: function(e){
						console.log('openDatabase failed: '+JSON.stringify(e));
					}
				});
		    // (id,name,bts,times)
			
		
			plus.sqlite.executeSql({
				name: 'first',
				sql: 'create table if not exists database("name" CHAR(110),"bts" CHAR(110),"id" CHAR(110), "times" CHAR(110))',
				success: function(e){
					console.log('executeSql success!');
					/**
					plus.sqlite.executeSql({
						name: 'first',
						sql: "insert into database values('磁力','XXX','1','2000')",
						success: function(e){
							console.log('executeSql success!');
							plus.sqlite.selectSql({
								name: 'first',
								sql: "select * from database",
								success: function(data){
									var len = data.length, i;
									for (i = 0; i < len; i++){
										console.log(data[i])
									   //self.list.push(data[i])
									}
								},
								fail: function(e){
									console.log('selectSql failed1: '+JSON.stringify(e));
								}
							});
						},
						fail: function(e){
							console.log('executeSql failed2: '+JSON.stringify(e));
						}
					});**/
				},
				fail: function(e){
					console.log('executeSql failed: '+JSON.stringify(e));
				}
			});
		  
		},
		mounted() {
		    // this.timer = setInterval(this.get, 1000);
		},
		beforeDestroy () {
            //clearInterval(this.timer);
        },
	    destroyed () {
			plus.sqlite.closeDatabase({
					name: 'first',
					success: function(e){
						console.log('closeDatabase success!');
					},
					fail: function(e){
						console.log('closeDatabase failed: '+JSON.stringify(e));
					}
				});	
        },
		methods: {
			navToBts(){
				uni.navigateTo({
					url: '/pages/index/bt'
				});
			},
			adds(){
				let self=this
				self.shows=1
			},
			closeadd(){
				let self=this
				self.shows=2
			},
			saves(){
				let self=this
				if(!self.url){
					uni.showToast({
						title: '请输入网址',
						icon: 'none',
						duration: 2000
					});
					return
				}
				if(!self.name){
					uni.showToast({
						title: '请输入网站名字',
						icon: 'none',
						duration: 2000
					});
					return
				}
				let info={
					url:self.url,
					name:self.name,
					status:'1'
				}
				self.btlist.push(info)
				uni.setStorageSync('btlist',self.btlist)
				self.shows=2
			},
			dels(val,index){
				let self=this
				self.btlist.splice(self.btlist.indexOf(index), 1); //取消
				uni.setStorageSync('btlist',self.btlist)
			},
			navTos(item){
				uni.navigateTo({
					url: '/pages/index/search?url='+item.url+"&name="+item.name
				});
			},
			get() {
				let self=this
				uni.getClipboardData({
				    success: function (res) {
					    self.text=res.data
						console.log(self.text)
				    }
				});
			},
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #FFFFFF;
	}
	
	.index-content {
		width: 100%;
		overflow: hidden;
		margin: 0rpx auto;
		.moban{
			position: fixed;
			width: 100%;
			height: 100%;
			z-index: 99;
			background:rgba(0, 0, 0, 0.5);
		}
		.top-input{
			position: fixed;
			top: 20%;
		    left: 10%;
			width: 80%;
			overflow: hidden;
			line-height: 188rpx;
			z-index: 100;
			background: #FFFFFF;
			border-radius: 20rpx;
			box-shadow: 0px 0px 5px #E3E3E3;
			.top-title{
				width: 100%;
				overflow: hidden;
				line-height: 66rpx;
				text-align: center;
				margin: 20rpx auto;
			}
			.top-item{
				width: 100%;
				overflow: hidden;
				line-height: 66rpx;
				text-align: center;
				margin: 20rpx auto;
				.input{
					width: 95%;
					overflow: hidden;
					height: 66rpx;
					color: #333333;
					margin: 0rpx auto;
					border: 2rpx solid #333333;
					border-radius: 20rpx;
					padding-left: 20rpx;
				}
			}
			.submits{
				width: 98%;
				overflow: hidden;
				margin: 20rpx auto;
				line-height: 66rpx;
				text-align: center;
				font-size: 28rpx;
				background: #FF4b56;
				color: #FFFFFF;
				border-radius: 20rpx;
			}
		}
		.add-bts{
			width: 128rpx;
			height: 128rpx;
			background: url(../../static/add.png) no-repeat center;
			background-size: 100%;
			margin: 20rpx auto;
			
		}
		.centers{
			width: 100%;
			overflow: hidden;
			margin: 0rpx auto;
			.items{
				width: 90%;
				margin: 20rpx auto;
				line-height: 68rpx;
				background-color: #FFFFFF;
				text-align: center;
				border: 1rpx solid #333;
				border-radius: 18rpx;
			}
			.url-items{
				width: 90%;
				overflow: hidden;
				margin: 20rpx auto;
				.left{
					width: 80%;
					float: left;
					line-height: 68rpx;
					background-color: #FFFFFF;
					text-align: center;
					border: 1rpx solid #333;
					border-radius: 18rpx;
				}
				.dels{
					width: 68rpx;
					height: 68rpx;
					background: url(../../static/del.png) no-repeat center;
					background-size: 99%;
					float: right;
					display: inline-block;
				}
			}
		}
	}
</style>
